{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fa2c827c",
   "metadata": {},
   "source": [
    "--------\n",
    "\n",
    "* *记录人（学习人）：kaijun*\n",
    "\n",
    "* *时间：2021-11-25*\n",
    "\n",
    "* *我的笔记都是每周写好的！！！只是忘记上传而已！！！*\n",
    "\n",
    "--------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fed58ebb",
   "metadata": {},
   "source": [
    "## 本周内容\n",
    "#### 1. 函数的返回值\n",
    "#### 2. 函数的输入参数类型\n",
    "#### 3. 函数的返回参数类型\n",
    "#### 4. 模块的概念\n",
    "#### 5. 如何创建一个模块，并且调用自己定义的模块\n",
    "\n",
    "-------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2eb4c01",
   "metadata": {},
   "source": [
    "### 函数的返回值\n",
    "1. 返回值关键字：return\n",
    "2. 函数一定有返回值（无论是什么编程语言），并且返回值必须写出，若在python中不写返回值，就会返回默认值 None\n",
    "3. 返回值一般为函数体运行的结果（如函数返回值实践1）；也可以是自定义的函数返回值（如函数返回值实践2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1adb1347",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "    # print(full_name)\n",
    "    return full_name     # 👈retrun 返回 full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2a104c3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Li Hua'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_formatted_name(\"Li\",\"Hua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7453af69",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "09a0b5b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"lihua\",123456)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f413257",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e2b32c81",
   "metadata": {},
   "source": [
    "### 函数的输入参数的类型\n",
    "1. 函数可以输入复杂的参数：\n",
    " 1. 函数参数为字典\n",
    " 2. 函数参数为列表\n",
    " 3. .....等等\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5859c64a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_A(names):\n",
    "    \"\"\"测试函数参数输入为列表\"\"\"\n",
    "    return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3059aba4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Tom', 'Bob']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_A([\"Lihua\",\"Alex\",\"Tom\",\"Bob\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f1722ef1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_B(person):\n",
    "    \"\"\"测试函数参数输入为字典\"\"\"\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d999ef14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu_id': 2010130000, 'age': 19, 'job': 'teacher'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_B({\"stu_id\":2010130000,\"age\":19,\"job\":\"teacher\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2bb0c61",
   "metadata": {},
   "source": [
    "2. 函数可以输入不确定个数的参数：\n",
    " 1. *args\n",
    "  - args 可以接受多个数据（单一值）\n",
    "  - args 将多个数据存在一个**元组**当中，实际上还是一个变量  \n",
    "  \n",
    "  \n",
    " 2. **kwargs\n",
    "  - kwargs 可以接受多个数据（键值对）\n",
    "  - kwargs 将多个 键值对 的数据存放在一个**字典**中，实际上还是一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1f8b1c21",
   "metadata": {},
   "outputs": [],
   "source": [
    "def food_ingred(food,*ingreds):\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5d6f4573",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred('pizza','mushroom','green peppers','extra cheese')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "da7cee59",
   "metadata": {},
   "outputs": [],
   "source": [
    "def person(stud_name,**stud_course):\n",
    "    return stud_name,stud_course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e7957a0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.person(stud_name, **stud_course)>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a480771",
   "metadata": {},
   "source": [
    "### 函数的输出参数类型\n",
    "1. 在编程语言中，函数的返回值只能是一个值\n",
    "2. 返回值可以是 字符串、数值、列表、字典等数据（单一值）\n",
    "3. 但如果return后面写多个值的话，将会返回一个元组（这是一个值，并没有违背函数在编程思维中的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9eb6e578",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return \"HELLO\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "59781496",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "139406d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return为数值\"\"\"\n",
    "    return 12345"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "90f78e2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b63627b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return为列表\"\"\"\n",
    "    return['Lihua','Alex','Tom']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "78e980b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Tom']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b6a17721",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return为字典\"\"\"\n",
    "    return {'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9215acbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b0813591",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test5_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 18,\"lihua\", ['Lihua','Alex','Tom'], {'stud_id': 210001, 'age': 18, 'Job': 'student'}      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "fc183b5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18,\n",
       " 'lihua',\n",
       " ['Lihua', 'Alex', 'Tom'],\n",
       " {'stud_id': 210001, 'age': 18, 'Job': 'student'})"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8baa5176",
   "metadata": {},
   "source": [
    "### 模块\n",
    "1. 模块的优势\n",
    " 1. 使用模块可以隐藏函数的源代码\n",
    " 2. 使用的人不需要会底层代码（只需要会使用模块即可）\n",
    " \n",
    " \n",
    "2. 模块的使用方法\n",
    " 1. import 模块名\n",
    "  - 使用方法：模块名.函数名()\n",
    "  \n",
    "  \n",
    " 2. from 模块名 import 函数名\n",
    "  - 使用方法：函数名()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "58534e7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3460b8d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 函数的调用\n",
    "study.search4vowels(\"hello,world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f1f527e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
